package com.auto.survey.activity.monitoring.adapter

import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.activity.ComponentActivity
import androidx.cardview.widget.CardView
import androidx.recyclerview.widget.RecyclerView.Adapter
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.auto.survey.R
import com.auto.survey.activity.monitoring.IntentKey.KEY_INTENT_OBJECT_ID
import com.auto.survey.activity.monitoring.IntentKey.KEY_INTENT_PROJET_ID
import com.auto.survey.activity.monitoring.MonitoringActivity
import com.auto.survey.activity.monitoring.entity.MonitoringObject

class MontoringObjectAdapter(activity: ComponentActivity) : Adapter<MontoringObjectAdapter.MontoringObjectAdapterViewHodler>() {

    private val mActivity = activity
    private var mItems :List<MonitoringObject> = mutableListOf()

    fun setItems(items: List<MonitoringObject>) {
        mItems =items
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): MontoringObjectAdapterViewHodler {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_montoring_project, parent, false)
        return MontoringObjectAdapterViewHodler(view)
    }

    override fun getItemCount(): Int {
        if (mItems.isNotEmpty()) {
            return mItems.size
        }
        return 0
    }

    override fun onBindViewHolder(holder: MontoringObjectAdapterViewHodler, position: Int) {
        val objName = "监测对象名称：${mItems[position].monitoringName}"
        holder.projectName.text = objName

        val time = "创建时间：${mItems[position].createTime}"
        holder.createTime.text = time

        holder.rootview.setOnClickListener {
            val intent = Intent()
            val obj = mItems[position]
            intent.putExtra(KEY_INTENT_PROJET_ID, obj.projectId)
            intent.putExtra(KEY_INTENT_OBJECT_ID,obj.id)
            intent.setClass(mActivity, MonitoringActivity::class.java)
            mActivity.startActivity(intent)
        }
    }

    class MontoringObjectAdapterViewHodler(itemview: View) : ViewHolder(itemview) {
        val rootview = itemview.findViewById<CardView>(R.id.cardview_item_montoring_project)
        val projectName = itemview.findViewById<TextView>(R.id.tv_item_montoring_project_name)
        val createTime = itemview.findViewById<TextView>(R.id.tv_item_montoring_project_creattime)
    }


}